package de.swta.freezay.admin.itemPackage;

import de.swta.freezay.database.JPAController;
import de.swta.freezay.database.dbEntities.Package;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;

/**
 *
 * @author Jan
 */
public abstract class PackageItemCheckBoxPanel extends Panel {

    /**
     * Creates a new instance of PackageItemCheckBoxPanel.
     *
     * @param id Id of the panel.
     * @param packageId PackageId.
     * @param itemId ItemId.
     * @param editMode if edit mode package is loaded.
     */
    public PackageItemCheckBoxPanel(String id, Integer packageId,
                                    Integer itemId, boolean editMode) {

        super (id);

        init(packageId, itemId, editMode);
    }

    /**
     * Initalizes the components - checkBoxPanel.
     *
     * @param packageId PackageId.
     * @param itemId ItemId.
     * @param editMode if edit mode package is loaded.
     */
    private void init(Integer packageId, Integer itemId, boolean editMode) {

        boolean contains = false;
        if (editMode) {
            Package p = JPAController.PackageJpaController.findPackage(packageId);
            contains = p.containsItem(itemId);
        }
        

        this.add(new AjaxCheckBox("checkBox", new Model<Boolean>(contains)) {

            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                PackageItemCheckBoxPanel.this.checkBoxValueChanged(
                              target, this.getModel().getObject());
            }
        });

    }

    /**
     * Handles the actions, if the value in checkBox changed.
     *
     * @param target AjaxRequestTarget
     * @param selected value if the Checkbox is selected.
     */
    public abstract void checkBoxValueChanged(AjaxRequestTarget target, Boolean selected);
}
